package com.ant.storage.controller;


import com.ant.storage.common.ResultVo;
import com.ant.storage.dto.AuditDto;
import com.ant.storage.pojo.AuditList;
import com.ant.storage.pojo.Picking;
import com.ant.storage.service.IAuditListService;
import com.ant.storage.service.IPickingService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author melon
 * @since 2022-03-25
 */
@RestController
@RequestMapping("/audit-list")
public class AuditListController {

    @Autowired
    private IAuditListService auditListService;

    @Autowired
    private IPickingService pickingService;

    @ApiOperation(value = "查看")
    @GetMapping("/findAuditList")
    public ResultVo findAuditList(AuditDto dto) {
        return ResultVo.success("操作成功", auditListService.findAuditList(dto));
    }


    @ApiOperation(value = "查看")
    @GetMapping("/findAuditLists")
    public ResultVo findAuditLists(AuditDto dto) {
        List<AuditList> list=null;
        QueryWrapper<AuditList> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(dto.getProcessType())) {
            wrapper.eq("process_type", dto.getProcessType());
            wrapper.orderByDesc("id");
            list = auditListService.list(wrapper);
        }
        return ResultVo.success("操作成功", list);
    }

    @ApiOperation(value = "查看")
    @GetMapping("/adoptAudit/{id}/{type}")
    public ResultVo adoptAudit(@PathVariable("id") Integer id, @PathVariable("type") Integer type) {
        if (type == 1) {
            pickingService.update(new UpdateWrapper<Picking>().set("audit_status", 2).eq("id", id));
            return ResultVo.success("操作成功");
        } else {
            auditListService.update(new UpdateWrapper<AuditList>().set("status", 2)
                    .eq("parent_id", id).eq("process_type", "picking"));
            pickingService.update(new UpdateWrapper<Picking>().set("audit_status", 2).eq("id", id));
            return ResultVo.success("操作成功");
        }
    }

    @ApiOperation(value = "获取上一次审批流程")
    @GetMapping("/finalApproval/{types}")
    public ResultVo finalApproval(@PathVariable("types") String types) {
        return ResultVo.success("操作成功", auditListService.finalApproval(types));
    }
    @ApiOperation(value = "获取上一次审批流程")
    @GetMapping("/finalApprovalFunds/{types}/{id}")
    public ResultVo finalApprovalFunds(@PathVariable("types") String types,@PathVariable("id") Integer id) {
        return ResultVo.success("操作成功", auditListService.finalApprovalFunds(types,id));
    }

}
